/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utility;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author Jeppe
 */
public class Resizeable {

    private final Rectangle originalBounds;
    private Rectangle newBounds;
    private final ArrayList<Rectangle> rectangleList;
    private boolean firstRun;
    private final JFrame mainGUI;

    public Resizeable(JFrame jFrame, Rectangle originalResolution) {
        this.mainGUI = jFrame;
        this.originalBounds = originalResolution;
        newBounds = originalResolution;
        rectangleList = new ArrayList<>();
        firstRun = true;
    }

    private void setNewResolution(Rectangle actualResolution) {
        newBounds = new Rectangle(actualResolution);
//        System.out.println("Nu er programmet " + actualResolution.width + "," + actualResolution.height + " pixels stort.");
    }

    private Dimension getMySize(Dimension originalSize) {
        double relatedXValue = getRelatedXValue();
        double relatedYValue = getRelatedYValue();
//        System.out.println("Forholdet på x-siden: " + relatedXValue);
//        System.out.println("Forholdet på y-siden: " + relatedYValue);

        int newXSize = (int) Math.round((double) originalSize.width * relatedXValue);
        int newYSize = (int) Math.round((double) originalSize.height * relatedYValue);
//        System.out.println("Ny størrelse når original var: " + originalSize.width + ", " + originalSize.height + "-> (" + newXSize + "," + newYSize + ")");
        return new Dimension(newXSize, newYSize);
    }

    private Point getMyLocation(Point location) {
        double relatedXValue = getRelatedXValue();
        double relatedYValue = getRelatedYValue();
        int newXValue = (int) Math.round((double) location.x * relatedXValue);
        int newYValue = (int) Math.round((double) location.y * relatedYValue);
//        System.out.println("Gammel Placering : (" + location.getX() + ", " + location.getY() + ") | Ny Placering : (" + newXValue + ", " + newYValue + ")");
        return new Point(newXValue, newYValue);
    }

    private double getRelatedXValue() {
        double relatedXValue = ((double) newBounds.width) / ((double) originalBounds.width);
        return relatedXValue;
    }

    private double getRelatedYValue() {
        double relatedYValue = ((double) newBounds.height) / ((double) originalBounds.height);
        return relatedYValue;
    }

//    public void addComponentToComponentList(JFrame jFrame) {
//        rectangleList.add(new Rectangle(jFrame.getBounds().x + mainGUI.getBounds().x, jFrame.getBounds().y + mainGUI.getBounds().y, jFrame.getBounds().height, jFrame.getBounds().width));
//        ArrayList<Component> componentList = (ArrayList<Component>) getAllComponents(jFrame.getRootPane());
//        for (Component component : componentList) {
//            rectangleList.add(component.getBounds());
//        }
//    }
    public void resize(JFrame[] jFrameList) {
        setNewResolution(mainGUI.getBounds());

        //<editor-fold defaultstate="collapsed" desc="Tilføjelse af Rectangles">
        ArrayList<Component> componentList = (ArrayList<Component>) getAllComponents(mainGUI.getRootPane());
        if (firstRun) {
            for (Component component : componentList) {
                rectangleList.add(component.getBounds()); // MULIG FEJL
            }

            for (JFrame jFrame : jFrameList) {
                rectangleList.add(new Rectangle(jFrame.getBounds().x + mainGUI.getBounds().x, jFrame.getBounds().y + mainGUI.getBounds().y, jFrame.getBounds().height, jFrame.getBounds().width));
                ArrayList<Component> componentList2 = (ArrayList<Component>) getAllComponents(jFrame.getRootPane());
                for (Component component : componentList2) {

                    rectangleList.add(component.getBounds());
                }
            }
            firstRun = false;
        }
        //</editor-fold>

        for (JFrame jFrame : jFrameList) {
            ArrayList<Component> componentList2 = (ArrayList<Component>) getAllComponents(jFrame.getRootPane());
            componentList.add(jFrame);
            System.out.println("JEG HAR FUNDET SKÆRMNUMPADET! og den er placeret i : (" + jFrame.getBounds().x + ", " + jFrame.getBounds().y + ")");
            for (Component component : componentList2) {
                componentList.add(component);
            }
        }
        resizeComponents(componentList);
        mainGUI.repaint();
        mainGUI.revalidate();
        for (JFrame jFrame : jFrameList) {
            jFrame.revalidate();
            jFrame.repaint();
        }
    }

    private void resizeComponents(ArrayList<Component> componentList) {
        for (int i = 0; i < componentList.size(); i++) {
            Component component = componentList.get(i);
            if (component instanceof JFrame) {
                
            } else {
//            System.out.println("i= " + i);
                Point newLocation = getMyLocation(rectangleList.get(i).getLocation());
                Dimension newDimension = getMySize(rectangleList.get(i).getSize());
                component.setBounds(newLocation.x, newLocation.y, newDimension.width, newDimension.height);
//            System.out.println(component.getClass().getName());
            }
        }
//        System.out.println("Number of components in MainGUI = "+ componentList.size());

    }

    private static List<Component> getAllComponents(final Container c) {
        Component[] comps = c.getComponents();
        List<Component> compList = new ArrayList<>();
        for (Component comp : comps) {
            compList.add(comp);
            if (comp instanceof Container) {
                compList.addAll(getAllComponents((Container) comp));
            }
        }
        return compList;
    }
}
